package com.tulele.vtool.handler;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import io.micrometer.core.instrument.util.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import com.tulele.vtool.model.BBusinessDict;
import com.tulele.vtool.service.BBusinessDictService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商学院字典表(BBusinessDict)表控制层
 *
 * @author makejava
 * @since 2020-10-22 15:35:27
 */
@RestController
@RequestMapping("bBusinessDict")
public class BBusinessDictHandler {
    /**
     * 服务对象
     */
    @Resource
    private BBusinessDictService bBusinessDictService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public BBusinessDict selectOne(Integer id) {
        return this.bBusinessDictService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param bBusinessDict 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/insert", produces = "application/json;charset=UTF-8")
    public RespData insert(@RequestHeader("userId") String userId, @RequestBody BBusinessDict bBusinessDict) {

        RespData respData = new RespData();

        if (null == bBusinessDict || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessDict", this.bBusinessDictService.insert(bBusinessDict));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("新增成功");
        return respData;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @PostMapping(value = "/deleteById", produces = "application/json;charset=UTF-8")
    public RespData deleteById(@RequestHeader("userId") String userId, @Param("id") Integer id) {

        RespData respData = new RespData();

        if (null == id || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("boolean", this.bBusinessDictService.deleteById(id));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("删除成功");
        return respData;
    }

    /**
     * 修改数据
     *
     * @param bBusinessDict 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/update", produces = "application/json;charset=UTF-8")
    public RespData update(@RequestHeader("userId") String userId, @RequestBody BBusinessDict bBusinessDict) {

        RespData respData = new RespData();

        if (null == bBusinessDict || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessDict", this.bBusinessDictService.update(bBusinessDict));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("修改成功");
        return respData;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @PostMapping(value = "/queryById", produces = "application/json;charset=UTF-8")
    public RespData queryById(@RequestHeader("userId") String userId, @Param("id") Integer id) {

        RespData respData = new RespData();

        if (null == id || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessDict", this.bBusinessDictService.queryById(id));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @PostMapping(value = "/queryAllByLimit", produces = "application/json;charset=UTF-8")
    public RespData queryAllByLimit(@RequestHeader("userId") String userId, @Param("offset") Integer offset, @Param("limit") Integer limit) {

        Integer pageNum = offset;
        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = 1;
        }

        Integer pageSize = limit;
        if (pageSize == null || pageSize.intValue() <= 0) {
            pageSize = 10;
        }

        RespData respData = new RespData();

        if (StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessDictList", this.bBusinessDictService.queryAllByLimit(pageNum, pageSize));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 查询多条数据
     *
     * @param bBusinessDict 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/selectAllByLimit", produces = "application/json;charset=UTF-8")
    public RespData selectAllByLimit(@RequestHeader("userId") String userId, @RequestHeader("identifying") String springProfilesActive, @RequestBody BBusinessDict bBusinessDict) {

        RespData respData = new RespData();

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(springProfilesActive) || bBusinessDict == null || bBusinessDict.getType() == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Integer pageNum = bBusinessDict.getPageNum();
        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = 1;
        }

        Integer pageSize = bBusinessDict.getPageSize();
        if (pageSize == null || pageSize.intValue() <= 0) {
            pageSize = 10;
        }

        bBusinessDict.setGzhCode(springProfilesActive);
        PageInfo<BBusinessDict> pageInfo = this.bBusinessDictService.selectAllByLimit(pageNum, pageSize, bBusinessDict);

        if (null == pageInfo.getList() || pageInfo.getList().size() <= 0) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("暂无更多");
            return respData;
        }
        List<BBusinessDict> queryAllByLimitDto = pageInfo.getList();
        if (CollectionUtils.isNotEmpty(queryAllByLimitDto)) {
            List<BBusinessDict> collect = queryAllByLimitDto.stream().map(info -> {

                BBusinessDict temporary = new BBusinessDict();
                temporary.setType(info.getType());
                temporary.setNumber(info.getNumber());
                temporary.setName(info.getName());
                temporary.setSork(info.getSork());

                return temporary;
            }).collect(Collectors.toList());

            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);
            resultMap.put("msg", pageInfo.isIsLastPage());
            resultMap.put("collect", collect);

            respData.setData(resultMap);
        }

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }


}